/**
 * @Time: 2018/11/22 14:18
 * @Author: wangmin
 * @File: block
 * @Software: GoLand
 */
package cluster

import (
	"open_manjing/microsys/jsonio"
	"open_manjing/oss/models/block"
	"github.com/astaxie/beego/orm"
	"open_manjing/oss/models"
	"open_manjing/oss/component/paging"
)

//存储块管理
type Block struct {
	App
}

// @Title 添加存储块
// @Description 添加存储块
// @Param Name  body string  true  "块名称"
// @Param Auth  body int     true  "权限类型（1、私有读写2、公共读私有写3、公共读写）"
// @Success 0     {}
// @Failure 50000 业务错误
// @Failure 50010 请求参数错误
// @router /add [post]
func (c *Block) Add() {
	var data struct {
		Name string
		Auth int8
	}
	ok, info := jsonio.Input(c.Ctx.Input.RequestBody, &data)
	if !ok {
		c.Data["json"] = jsonio.Fail(jsonio.ErrParamError, info, nil)
		return
	}
	o := orm.NewOrm()
	bm := block.Instance(o)
	err, _ := bm.Add(data.Name, data.Auth)
	if err != nil {
		c.Data["json"] = jsonio.Fail(jsonio.ErrBusiness, err.Error(), nil)
		return
	}
	c.Data["json"] = jsonio.Success(nil, "添加成功")
}

// @Title 列出存储块
// @Description 列出存储块
// @Param Page     body int     true  "页码"
// @Param PageSize body int     false  "每页数量20-1000"
// @Param Name     body string  false  "块名称检索"
// @Success 0     {Created:string,Name:string,Auth:int,AuthText:string}
// @Failure 50000 业务错误
// @Failure 50010 请求参数错误
// @router /list [post]
func (c *Block) List() {
	var data struct {
		Page     int
		PageSize int
		Name     string
	}
	ok, info := jsonio.Input(c.Ctx.Input.RequestBody, &data)
	if !ok {
		c.Data["json"] = jsonio.Fail(jsonio.ErrParamError, info, nil)
		return
	}
	o := orm.NewOrm()
	qs := o.QueryTable("Block")
	if data.Name != "" {
		qs.Filter("Name__istartswith", data.Name)
	}
	//查询总数
	count, _ := qs.Count()
	//查询列表
	page, offset, limit := paging.Calculation(data.Page, data.PageSize)
	list := make([]models.Block, data.PageSize)
	out := make([]map[string]interface{}, data.PageSize)
	_, err := qs.Offset(offset).Limit(limit).All(&list, "Created", "Name", "Auth")
	if err != nil {
		c.Data["json"] = jsonio.Fail(jsonio.ErrServerError, err.Error(), nil)
		return
	}
	for _, item := range list {
		out = append(out, map[string]interface{}{
			"Created":  item.Created.Format("2006-01-02 15:04:05"),
			"Name":     item.Name,
			"Auth":     item.Auth,
			"AuthText": block.GetStatusFieldText("Auth", item.Auth),
		})
	}
	c.Data["json"] = jsonio.Success(paging.PageUtilWithCount(int(count), page, limit, out), "")
}

// @Title 存储块信息
// @Description 存储块信息
// @Param Name body string  false  "块名称"
// @Success 0     {}
// @Failure 50000 业务错误
// @Failure 50010 请求参数错误
// @router /info [post]
func (c *Block) Info() {
	var data struct {
		Name string
	}
	ok, info := jsonio.Input(c.Ctx.Input.RequestBody, &data)
	if !ok {
		c.Data["json"] = jsonio.Fail(jsonio.ErrParamError, info, nil)
		return
	}
	o := orm.NewOrm()
	b := &models.Block{}
	err := o.Read(b, "Name")
	switch err {
	case nil:
		c.Data["json"] = jsonio.Success(struct {
			Created  string
			Name     string
			Auth     int8
			AuthText string
		}{
			Created:  b.Created.Format("2006-01-02 15:04:05"),
			Name:     b.Name,
			Auth:     b.Auth,
			AuthText: block.GetStatusFieldText("Auth", b.Auth),
		}, "")
		return
	case orm.ErrNoRows:
		c.Data["json"] = jsonio.Fail(jsonio.ErrNotFound, "块不存在", nil)
		return
	default:
		c.Data["json"] = jsonio.Fail(jsonio.ErrServerError, "服务器异常："+err.Error(), nil)
		return
	}
}

// @Title 重置权限级别
// @Description 重置权限级别
// @Param Name body string  false  "块名称"
// @Param Auth body int     false  "权限类型（1、私有读写2、公共读私有写3、公共读写）"
// @Success 0     {}
// @Failure 50000 业务错误
// @Failure 50010 请求参数错误
// @router /reset-auth [post]
func (c *Block) ResetAuth() {
	var data struct {
		Name string
		Auth int8
	}
	ok, info := jsonio.Input(c.Ctx.Input.RequestBody, &data)
	if !ok {
		c.Data["json"] = jsonio.Fail(jsonio.ErrParamError, info, nil)
		return
	}
	o := orm.NewOrm()
	bm := block.Instance(o)
	err := bm.ResetAuth(data.Name, data.Auth)
	if err != nil {
		c.Data["json"] = jsonio.Fail(jsonio.ErrBusiness, err.Error(), nil)
		return
	}
	c.Data["json"] = jsonio.Success(nil, "权限设置成功")
}

// @Title 删除存储块
// @Description 删除存储块
// @Param Name  body string  true  "块名称"
// @Success 0     {}
// @Failure 50000 业务错误
// @Failure 50010 请求参数错误
// @router /remove [post]
func (c *Block) Remove() {
	var data struct {
		Name string
	}
	ok, info := jsonio.Input(c.Ctx.Input.RequestBody, &data)
	if !ok {
		c.Data["json"] = jsonio.Fail(jsonio.ErrParamError, info, nil)
		return
	}
	o := orm.NewOrm()
	bm := block.Instance(o)
	err := bm.Del(data.Name)
	if err != nil {
		c.Data["json"] = jsonio.Fail(jsonio.ErrBusiness, err.Error(), nil)
		return
	}
	c.Data["json"] = jsonio.Success(nil, "删除成功")
}
